package com.pope.framework.rescue.modular.service;

import com.pope.framework.constants.RescueWhereAboutsEnum;
import com.pope.framework.core.util.StringUtil;
import com.pope.framework.dict.modular.entity.DictComplaintsD;
import com.pope.framework.dict.modular.service.DictComplaintsDService;
import com.pope.framework.kernel.model.exception.ServiceException;
import com.pope.framework.rescue.modular.dto.RescueInfoDto;
import com.pope.framework.rescue.modular.entity.RescueInfo;
import com.pope.framework.rescue.modular.entity.RescueLog;
import com.pope.framework.rescue.modular.mapper.RescueInfoMapper;
import com.pope.framework.third.ThirdUtil;
import com.pope.framework.triage.modular.entity.ComplaintsInfo;
import com.pope.framework.triage.modular.entity.TriageInfo;
import com.pope.framework.triage.modular.service.ComplaintsInfoService;
import com.pope.framework.triage.modular.service.TriageInfoService;
import com.pope.framework.triage.modular.vo.TriageSimpleInfoVo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

import java.text.DateFormat;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 离抢信息 服务实现类
 * </p>
 *
 * @author superpope
 * @since 2019-01-16
 */
@Service
@Transactional
public class RescueInfoService extends ServiceImpl<RescueInfoMapper, RescueInfo> {

	@Autowired
	private ThirdUtil thirdUtil;
	@Autowired
	private RescueLogService rescueLogService;
	@Autowired
	private TriageInfoService triageInfoService;
	@Autowired
	private ComplaintsInfoService complaintsInfoService; 
	@Autowired
	private DictComplaintsDService dictComplaintsDService;
	
	//调整列表没有查询出来主诉信息
	public IPage<TriageSimpleInfoVo> listSimple(Page<TriageSimpleInfoVo> page, String name, String beginDate,
			 String endDate,String triageState,Integer hospitalId,String emergencyNumber){
		DictComplaintsD queryDictComplaintsD=new DictComplaintsD();
		 QueryWrapper<DictComplaintsD> queryDictComplaintsDWrapper=new QueryWrapper<>();
		 queryDictComplaintsDWrapper.setEntity(queryDictComplaintsD);
		 queryDictComplaintsDWrapper.orderByAsc("orders");
		 
		 List<DictComplaintsD> dictComplaintsDs=dictComplaintsDService.list(queryDictComplaintsDWrapper);
		 IPage<TriageSimpleInfoVo> datas=baseMapper.selectSimpleList(page, name, beginDate, endDate, triageState,hospitalId,emergencyNumber);
		 if(CollUtil.isNotEmpty(datas.getRecords())) {
			 List<Map<String,Object>> departs=thirdUtil.getDeparts(hospitalId);
			 for(TriageSimpleInfoVo vo:datas.getRecords()) {
				 vo.setDepartName(thirdUtil.getDepartName(departs, vo.getDepartId()));
				 vo.setComplaintNames(triageInfoService.getComplaintNames(vo.getId(), dictComplaintsDs));
			 }
		 }
		return datas;
	}
	public Integer insert(RescueInfoDto rescueInfoDto) {
		if(rescueInfoDto==null) {
			throw new ServiceException("离抢信息不能为空，请重写确认！");
		}
		RescueInfo rescueInfo=new RescueInfo();
		if(rescueInfoDto.getId()==null && rescueInfoDto.getTriageId()!=null) {
			RescueInfo queryRescueInfo=new RescueInfo();
			queryRescueInfo.setTriageId(rescueInfoDto.getTriageId());
			QueryWrapper<RescueInfo> queryRescueInfoWrapper=new QueryWrapper<>();
			queryRescueInfoWrapper.setEntity(queryRescueInfo);
			RescueInfo oldRescueInfo=this.getOne(queryRescueInfoWrapper);
			if(oldRescueInfo!=null) {
				rescueInfoDto.setId(oldRescueInfo.getId());
			}
			
		}
		BeanUtil.copyProperties(rescueInfoDto, rescueInfo);
		TriageInfo triageInfo=triageInfoService.getById(rescueInfo.getTriageId());
		if(triageInfo==null) {
			throw new ServiceException("该离抢信息对应的分诊记录已不存在！");
		}
		
		rescueInfo.setDepart(triageInfo.getDepart());
		
		//rescueInfo.setGrade(triageInfo.getChangeGrade());
		if(triageInfo.getChangeGrade()!=null) {
			rescueInfo.setGrade(triageInfo.getChangeGrade());
		}else {
			if(triageInfo.getComingGrade()!=null) {
				rescueInfo.setGrade(triageInfo.getComingGrade());
			}else {
				rescueInfo.setGrade(triageInfo.getAutoGrade());
			}
		}
		if(rescueInfo.getId()==null) {
			rescueInfo.setEmergencyNumber(triageInfo.getEmergencyNumber());
			
			rescueInfo.setPatientId(triageInfo.getPatientId());
			rescueInfo.setVisitTime(triageInfo.getCreatetime());
			this.save(rescueInfo);
		}else {
			rescueInfo.setCreatetime(null);
			rescueInfo.setCreateMan(null);
			this.updateById(rescueInfo);
		}
		RescueLog queryRescueLog=new RescueLog();
		queryRescueLog.setRescueId(rescueInfo.getId());
		
		QueryWrapper<RescueLog> queryWrapper=new QueryWrapper<>();
		queryWrapper.setEntity(queryRescueLog);
		queryWrapper.orderByDesc("CREATETIME");
		RescueLog rescueLog=rescueLogService.getOne(queryWrapper);
		if(rescueLog!=null) {
			rescueLog.setEndTime(rescueInfo.getRescueTime());
			rescueLogService.updateById(rescueLog);
		}
		RescueLog saveRescueLog=new RescueLog();
		BeanUtil.copyProperties(rescueInfo, saveRescueLog);
		saveRescueLog.setId(null);
		saveRescueLog.setBeginTime(rescueInfo.getRescueTime());
		saveRescueLog.setRescueId(rescueInfo.getId());
		rescueLogService.save(saveRescueLog);
		return rescueInfo.getId();
	}
	
	
	
	public Integer selectPatientsNumber(String beginDate, String endDate,Integer hospitalId,RescueWhereAboutsEnum whereAbout) {
		
		QueryWrapper<RescueInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.between("LEFT(RESCUE_TIME,10)", beginDate, endDate);
		if(hospitalId!=null) {
			queryWrapper.eq("HOSPITAL_ID", hospitalId);
		}
		if(whereAbout!=null) {
			queryWrapper.eq("RESCUE_WHERE_ABOUTS", whereAbout.getCode());
		}
		
		return this.count(queryWrapper);
	}
	
	
	/**
	 * 按照部门统计部门病人数
	 * @param beginDate
	 * @param endDate
	 * @param hospitalId
	 * @param whereAbout
	 * @return
	 */
	public List<Map<String,Object>> selectDepartPatientsNumber(String beginDate, String endDate,Integer hospitalId,RescueWhereAboutsEnum whereAbout) {
		return baseMapper.selectDepartPatientsNumber(beginDate, endDate, hospitalId, whereAbout.getCode());
	}
}
